﻿//-----------------------------------------------------------------------------
//<filedescription file="IntegrationKit.Logging.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     Provides utility methods specific to logging of status messages,
//     error messages, and exceptions
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.Globalization;
using System.IO;
using Microsoft.ConfigurationManagement.AdminConsole;
using Microsoft.ConfigurationManagement.ManagementProvider;


namespace Microsoft.ConfigurationManagement.IntegrationDevelopmentKit
{
    /// <summary>
    /// 
    /// </summary>
    public partial class IntegrationKitUtilities
    {
        /// <summary>
        /// Writes detailed exception information to a log file for debugging.
        /// </summary>
        /// <param name="logFile">A StreamWriter object for the log file to write to</param>
        /// <param name="exceptionInfo">An Exception object passed from the location where the exception occurred</param>
        /// <param name="debugEnabled">A boolean value indicating whether or not debug logging is enabled</param>
        public static void LogException(StreamWriter logFile, Exception exceptionInfo, bool debugEnabled)
        {
            if (logFile == null)
            {
                return;
            }
            if (exceptionInfo == null)
            {
                return;
            }

            if (debugEnabled)
            {
                string exceptionMessage = "\n\n" +
                   "\n###############################################################################" +
                   "\n###############################################################################" +
                   "\nException encountered:" +
                   "\n\nType:\t" + exceptionInfo.GetType().Name +
                   "\n\nMessage:\t" + exceptionInfo.Message +
                   "\n\nSource:\t" + exceptionInfo.Source +
                   "\n\nData:\t" + exceptionInfo.Data +
                   "\n\nTargetSite:\t" + exceptionInfo.TargetSite +
                   "\n\nInnerException:\n" + exceptionInfo.InnerException +
                   "\n\nStackTrace:\n" + exceptionInfo.StackTrace + 
                   "\n###############################################################################" +
                   "\n###############################################################################\n\n";

                //DialogHelper.ShowMessageBox(exceptionMessage);

                try
                {
                    logFile.WriteLine(exceptionMessage);
                }
                catch (Exception e)
                {
                    DialogHelper.ShowMessageBox("Error writing to log file!\n" + e.Data + "\n" + e.InnerException + "\n" + e.StackTrace + "\n" + e.Message + "\n" + e.Source);

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logFile"></param>
        /// <param name="exceptionInfo"></param>
        public static void LogException(StreamWriter logFile, Exception exceptionInfo)
        {
            LogException(logFile, exceptionInfo, true);
        }

        /// <summary>
        /// Writes a string to a log file for debugging.
        /// </summary>
        /// <param name="logFile">A StreamWriter object for the log file to write to</param>
        /// <param name="outputText">A string to be written to the log file</param>
        /// <param name="debugEnabled">A boolean value indicating whether or not debug logging is enabled</param>
        public static void LogLine(StreamWriter logFile, string outputText, bool debugEnabled)
        {
            if (logFile == null)
            {
                //Console.WriteLine(outputText); 
                return;
            }
            if (string.IsNullOrEmpty(outputText))
            {
                return;
            }
            //if (debugEnabled)
            //{
                try
                {
                    logFile.WriteLine(DateTime.Now.ToString() + " :: " + outputText);
                }
                catch (Exception e)
                {
                    DialogHelper.ShowMessageBox("Error writing to log file!\n" + e.Data + "\n" + e.InnerException + "\n" + e.StackTrace + "\n" + e.Message + "\n" + e.Source);

                }
            //}
        }

        /// <summary>
        /// Writes detailed information about an object to a log file for debugging.
        /// </summary>
        /// <param name="logFile">A StreamWriter object for the log file to write to</param>
        /// <param name="outputText">A string to be written to the log file</param>
        /// <param name="obj">The object for which data will be logged</param>
        /// <param name="debugEnabled">A boolean value indicating whether or not debug logging is enabled</param>
        public static void LogLine(StreamWriter logFile, string outputText, IResultObject obj, bool debugEnabled)
        {
            //if (debugEnabled)
            //{
                try
                {
                    logFile.WriteLine(DateTime.Now.ToString() + " :: " + outputText);
                    logFile.WriteLine(DateTime.Now.ToString() + " :: Object Info:");
                    logFile.WriteLine(DateTime.Now.ToString() + " ::\t" + obj.ToString());
                    logFile.WriteLine(DateTime.Now.ToString() + " ::\t" + obj.DisplayDescription);
                    logFile.WriteLine(DateTime.Now.ToString() + " ::\t" + obj.DisplayString);
                    foreach (string propertyName in obj.PropertyNames)
                    {
                        logFile.WriteLine(DateTime.Now.ToString() + " :: " + propertyName + " : " + GetROStringProperty(obj, propertyName));
                    }
                }
                catch (Exception e)
                {
                    DialogHelper.ShowMessageBox("Error writing to log file!\n" + e.Data + "\n" + e.InnerException + "\n" + e.StackTrace + "\n" + e.Message + "\n" + e.Source);

                }
            //}
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="logFile" type="System.IO.StreamWriter">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="outputText" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="debugEnabled" type="bool">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        public static void LogStream(StreamWriter logFile, string outputText, bool debugEnabled)
        {
            if (logFile == null)
            {
                //Console.WriteLine(outputText); 
                return;
            }
            if (string.IsNullOrEmpty(outputText))
            {
                return;
            }
            //if (debugEnabled)
            //{
            try
            {
                string[] outputLines = outputText.Split(new char[] { '\n' });
                foreach (string outputLine in outputLines)
                {
                    logFile.WriteLine(DateTime.Now.ToString().Trim() + " :: " + outputLine.Trim());
                }
            }
            catch (Exception e)
            {
                DialogHelper.ShowMessageBox("Error writing to log file!\n" + e.Data + "\n" + e.InnerException  + "\n" + e.StackTrace  + "\n" +  e.Message  + "\n" + e.Source);

            }
            //}
        }


    }
}